function getItem(id, url) {
    return new Promise((resolve) => {
        let item = {
            id: id,
            image: `https://ssyer.oss-cn-hangzhou.aliyuncs.com/${url}`,
        };

        let image = new Image();
        image.src = item.image;
        image.addEventListener('load', () => {
            resolve(item);
        });
        image.addEventListener('error', () => {
            item.image = '';
            resolve(item);
        });
    });
}

import axios from 'axios';
const url = 'https://mobile.ssyer.com/picture/index?size=10&pageNo=';
let result = [];
let len = 0;

function getDataFromNet(limit, skip) {
    return new Promise((resolve, reject) => {
        axios.get(`${url}1`)
            .then(function(response) {
                result = result.concat(response.data.data);
                len = result.length;
                resolve(limit, skip);
            })
            .catch(function(error) {
                console.log(error);
            });
    });
}

function buildData(limit, skip) {
    return new Promise((resolve, reject) => {
        //   const total = limit + skip;
        let items = [];
        for (let i = 0; i < len; i++) {
            const item = result[i];
            items[i] = getItem(item.id, item.url);
        }
        resolve(Promise.all(items));
    }).then((list) => {
        return {
            list: list,
            count: 1000,
        };
    });
}

export function fetch(limit, skip) {
    const totalNeed = limit + skip;
    if (len < totalNeed) {
        return getDataFromNet(limit, skip).then(buildData);
    }
    return buildData(limit, skip);
}